<template>
  <div v-for="(course, index) in classes" :key="index">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>{{ course.className }}</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="showTable[index] = !showTable[index]">显示/隐藏表格</el-button>
      </div>
      <el-table v-if="showTable[index]" :data="course.assistants" style="width: 100%">
        <el-table-column fixed prop="teacherNo" label="teacherno" width="300" />
        <el-table-column prop="name" label="Name" width="300" />
        <el-table-column fixed="right" label="Operations" width="200">
          <template #default="{ row }">
            <el-button link type="primary" size="small" @click="deleteRow(index, row)">delete</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-top: 20px;">
        <el-input v-model="course.newAssistant.teacherNo" placeholder="请输入工号">
          <template #prepend>工号</template>
        </el-input>
        <el-input v-model="course.newAssistant.name" placeholder="请输入姓名" style="margin-top: 10px;">
          <template #prepend>姓名</template>
        </el-input>
        <el-button type="primary" style="margin-top: 10px;" @click="addRow(index)">提交</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showTable: [],
      classes: [
        {
          className: 'Math',
          newAssistant: {
            teacherNo: '',
            name: '',
          },
          assistants: [
            {
              teacherNo: '111',
              name: 'Tom',
            },
            {
              teacherNo: '222',
              name: 'Jerry',
            },
            // 更多助教...
          ],
        },
        {
          className: 'English',
          newAssistant: {
            teacherNo: '',
            name: '',
          },
          assistants: [
            {
              teacherNo: '333',
              name: 'Alice',
            },
            {
              teacherNo: '444',
              name: 'Bob',
            },
            // 更多助教...
          ],
        },
        // 更多课堂...
      ],
      // 其他数据...
    }
  },
  methods: {
    addRow(index) {
      this.classes[index].assistants.push({
        teacherNo: this.classes[index].newAssistant.teacherNo,
        name: this.classes[index].newAssistant.name,
      });
      this.classes[index].newAssistant.teacherNo = '';
      this.classes[index].newAssistant.name = '';
    },
    deleteRow(classIndex, assistant) {
      const index = this.classes[classIndex].assistants.indexOf(assistant);
      if (index !== -1) {
        this.classes[classIndex].assistants.splice(index, 1);
      }
    },
  },
}
</script>
